package com.jshoperxms.service.impl;

import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.dao.SysModuleActionTDao;
import com.jshoperxms.dao.SysModuleTDao;
import com.jshoperxms.entity.SysModuleActionT;
import com.jshoperxms.entity.SysModuleT;
import com.jshoperxms.service.SysModuleTService;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by sdywcd on 16/5/31.
 * Des:
 */

@Service("sysModuleTService")
@Scope("prototype")
public class SysModuleTServiceImpl extends BaseTServiceImpl<SysModuleT> implements SysModuleTService {

    @Resource
    private SysModuleTDao sysModuleTDao;

    @Resource
    private SysModuleActionTDao sysModuleActionTDao;

    @Transactional(propagation= Propagation.REQUIRED)
    @Override
    public void delSysModuleTransactional(String[] ids) {
        for(String s:ids){
            SysModuleT sysModuleT=this.sysModuleTDao.findByPK(SysModuleT.class,s);
            if(sysModuleT!=null){
                sysModuleT.setStatus(BaseEnums.DataUsingState.DEL.getState());
                sysModuleT.setUpdatetime(DateTools.getSystemTimeBi());
                sysModuleT.setVersiont(sysModuleT.getVersiont()+1);
                this.sysModuleTDao.update(sysModuleT);
                Criterion criterion= Restrictions.eq("sysmoduleid",sysModuleT.getId());
                List<SysModuleActionT> list=this.sysModuleActionTDao.findByCriteria(SysModuleActionT.class,criterion);
                if(!list.isEmpty()){
                    for(SysModuleActionT sys:list){
                        sys.setUpdatetime(DateTools.getSystemTimeBi());
                        sys.setVersiont(sys.getVersiont()+1);
                        sys.setStatus(BaseEnums.DataUsingState.DEL.getState());
                        this.sysModuleActionTDao.update(sys);
                    }

                }

            }
        }
    }

    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public void updateSysModuleTransactional(SysModuleT sysModuleT) {
        SysModuleT bean=this.sysModuleTDao.findByPK(SysModuleT.class,sysModuleT.getId());
        if(bean!=null){
            bean.setSysmodulename(sysModuleT.getSysmodulename());
            bean.setNote(sysModuleT.getNote());
            bean.setStatus(sysModuleT.getStatus());
            bean.setUpdatetime(DateTools.getSystemTimeBi());
            bean.setVersiont(bean.getVersiont()+1);
            this.sysModuleTDao.update(bean);
            Criterion criterion= Restrictions.eq("sysmoduleid",sysModuleT.getId());
            List<SysModuleActionT> list=this.sysModuleActionTDao.findByCriteria(SysModuleActionT.class,criterion);
            if(!list.isEmpty()){
                for(SysModuleActionT sys:list){
                    sys.setUpdatetime(DateTools.getSystemTimeBi());
                    sys.setVersiont(sys.getVersiont()+1);
                    sys.setSysmodulename(sysModuleT.getSysmodulename());
                    sys.setSysmoduleid(sysModuleT.getId());
                    sys.setStatus(sysModuleT.getStatus());
                    this.sysModuleActionTDao.update(sys);
                }
            }
        }
    }
}
